package com.open.capacity.reimburse.controller;

import java.util.Map;

import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.BillCarApply1;
import com.open.capacity.reimburse.service.BillCarApply1Service;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 公务车借车申请
 *
 * @author
 * @email
 * @date 2021-03-18 15:47:38
 */
@RestController
@RequestMapping("carapply1s")
@Api(tags = "公务车借车申请")
public class BillCarApply1Controller {

    @Autowired
    private BillCarApply1Service billCarApplyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('bill_car_apply:billcarapply:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = billCarApplyService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('bill_car_apply:billcarapply:list')")
    public Result findById(String id) {
        return Result.succeed(billCarApplyService.findById(id));
    }


    /**
     * 查询单条记录
     */
    @RequestMapping("/findByProcessId")
    @PreAuthorize("hasAnyAuthority('bill_car_apply:billcarapply:list')")
    public Result findByProcessId(String processId) {
        return Result.succeed(billCarApplyService.findById(processId));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('bill_car_apply:billcarapply:save')")
    public Result save(@RequestBody @Valid BillCarApply1 bill, String keyValue, Integer force) {
        if(StringUtils.isEmpty(keyValue)) {
            billCarApplyService.save(bill);
        }else {
            bill.setId(keyValue);
            billCarApplyService.update(bill);
        }
        return Result.succeed("操作成功");
    }


    /**
     * 还车
     */
    @RequestMapping("/back")
    @PreAuthorize("hasAnyAuthority('bill_car_apply:billcarapply:list')")
    public Result back(@RequestBody @Valid BillCarApply1 backDto, String keyValue) {
        billCarApplyService.back(backDto, keyValue);
        return Result.succeed("还车成功");
    }

}
